function Room(eventSource) {
	this.publicBlogs = {};
	this.map = null;
	this.marker = null;
	this.circle = null;
	this.rectangle = null;
	this.polyline = null;
	this.polygon = null;
	this.selected=null;
	this.eventSource = eventSource;

	this.adapter = new ClientAdapter("/CoupledObjectWebServer/");
	this.personalAdapter = new ClientAdapter("/CoupledObjectWebServer/");
	// this.adapter = new ClientAdapter("/StandaloneServer/");
	this.adapter.joinSession("feed");
	this.personalAdapter.joinSession("sync");
	var userId = this.personalAdapter.userId
	this.personalAdapter.leaveSession();
	this.personalAdapter.joinSession("intop-" + userId);
	this.adapter.start(500);
	this.personalAdapter.start(500);
	this.couple();
}

Room.prototype.getNewId = function() {
	return Math.floor((Math.random() * 100000) + 1) + '_'
			+ this.adapter.clientId;
};

Room.prototype.deleteOverlay = function(overlay) {
	if (overlay == 'circle')
		this.circle.setVisible(false);
	else if (overlay == 'rectangle')
		this.rectangle.setVisible(false);
	else if (overlay == 'polyline')
		this.polyline.setVisible(false);
	else if (overlay == 'polygon')
		this.polygon.setVisible(false);
	else if (overlay == 'marker')
		this.marker.setVisible(false);
	drawingControl(true);
};

Room.prototype.addBlog = function(marker) {
	this.marker = marker;
	listenerAdd('marker');
	google.maps.event.addListener(marker, 'click', function() {
		listenerAdd('marker');
	});
};

Room.prototype.addBlogCircle = function(circle) {
	this.circle = circle;
	listenerAdd('circle');
	google.maps.event.addListener(circle, 'click', function() {
		listenerAdd('circle');
	});
};

Room.prototype.addBlogPolygon = function(polygon) {
	this.polygon = polygon;
	listenerAdd('polygon');
	google.maps.event.addListener(polygon, 'click', function() {
		listenerAdd('polygon');
	});
};

Room.prototype.addBlogPolyline = function(polyline) {
	this.polyline = polyline;
	listenerAdd('polyline');
	google.maps.event.addListener(polyline, 'click', function() {
		listenerAdd('polyline');
	});
};

Room.prototype.addBlogRectangle = function(rectangle) {
	this.rectangle = rectangle;
	listenerAdd('rectangle');
	google.maps.event.addListener(rectangle, 'click', function() {
		listenerAdd('rectangle');
	});
};

Room.prototype.publishBlog = function(files) {
	this.marker.setVisible(false);
	drawingControl(true);
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publish(blogId, this.marker.getPosition().lat(), this.marker
			.getPosition().lng(), $("#title").val(), $("#description").val(),
			files, time, $('#userName').val());
};

Room.prototype.publishBlogCircle = function(files) {
	this.circle.setVisible(false);
	drawingControl(true);
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishCircle(blogId, this.circle.getCenter().lat(), this.circle
			.getCenter().lng(), this.circle.getRadius(), $("#title").val(), $(
			"#description").val(), files, time, $('#userName').val());
};

Room.prototype.publishBlogRectangle = function(files) {
	this.rectangle.setVisible(false);
	drawingControl(true);
	var sw = this.rectangle.getBounds().getSouthWest();
	var ne = this.rectangle.getBounds().getNorthEast();
	var boundArray = [ sw.lat(), sw.lng(), ne.lat(), ne.lng() ];
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishRectangle(blogId, boundArray, $("#title").val(), $(
			"#description").val(), files, time, $('#userName').val());
};

Room.prototype.publishBlogPolyline = function(files) {
	this.polyline.setVisible(false);
	drawingControl(true);
	var pathArray = this.polyline.getPath().getArray();
	var array = [];
	var length = this.polyline.getPath().getLength();
	for (var i = 0, j = 0; i < length; i++, j = j + 2) {
		array[j] = pathArray[i].lat();
		array[j + 1] = pathArray[i].lng();
	}
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishPolyline(blogId, array, length, $("#title").val(), $(
			"#description").val(), files, time, $('#userName').val());
};

Room.prototype.publishBlogPolygon = function(files) {
	this.polygon.setVisible(false);
	drawingControl(true);
	var pathArray = this.polygon.getPath().getArray();
	var array = [];
	var length = this.polygon.getPath().getLength();
	for (var i = 0, j = 0; i < length; i++, j = j + 2) {
		array[j] = pathArray[i].lat();
		array[j + 1] = pathArray[i].lng();
	}
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishPolygon(blogId, array, length, $("#title").val(), $(
			"#description").val(), files, time, $('#userName').val());
};

Room.prototype.publish = function(blogId, lat, lng, title, description, files,
		tags, time, userId) {
	this._doPublish(blogId, title, description, files, time, userId);
};
Room.prototype._doPublish = function(blogId, title, description, files, time,
		userId) {
	var ds = time;
	var hour = ds.split(":")[0];

	var minute = ds.split(":")[1];

	var second = ds.split(":")[2].split(" ")[0];

	var last_part2 = ds.split(" ")[1], day = last_part2.split("/")[0], month = last_part2
			.split("/")[1], year = last_part2.split("/")[2];

	this.publicBlogs[blogId] = new Blog(null, blogId, title, description, null,
			userId, new Date(year, parseInt(month) - 1, day, hour, minute,
					second));
	var jsonToLoad = {
		'dateTimeFormat' : 'iso8601',
		'events' : []
	};
	jsonToLoad.events.push(this._blogEvent(this.publicBlogs[blogId]));
	this.eventSource.loadJSON(jsonToLoad, ".");
	tl.layout();

};

Room.prototype._blogEvent = function(blog) {
	var event = {
		'start' : blog.date.toISOString(),
		'title' : blog.title,
		'description' : blog.description,
		'image' : blog.media,
		'blogId' : blog.id

	};
	return event;
};

Room.prototype.publishCircle = function(blogId, lat, lng, radius, title,
		description, files, tags, time, userId) {
	this._doPublish(blogId, title, description, files, time, userId);
};

Room.prototype.publishRectangle = function(blogId, boundsArray, title,
		description, files, tags, time, userId) {
	this._doPublish(blogId, title, description, files, time, userId);
};

Room.prototype.publishPolyline = function(blogId, pathArray, length, title,
		description, files, tags, time, userId) {
	this._doPublish(blogId, title, description, files, time, userId);
};

Room.prototype.publishPolygon = function(blogId, pathArray, length, title,
		description, files, tags, time, userId) {
	this._doPublish(blogId, title, description, files, time, userId);
};

Room.prototype.addCommentRoom = function(blogId, body, userName, time) {
	for (var i = 0; i < this.publicBlogs.length; i++) {
		if (this.publicBlogs[i].id == blogId) {
			this.publicBlogs[i].addComment(userName, body, time);
			addOneComment(blogId, userName, body, time);
			break;
		}
	}
};

Room.prototype.addLike = function(blogId) {
	// addLikeByBlogId(blogId);
};
Room.prototype.addDislike = function(blogId) {
	// addDislikeByBlogId(blogId);
};

Room.prototype.couple = function() {
	this.adapter.coupleObject("room", this, {
		messageType : "EVENT",
		explicitMapping : [ "publish", "addCommentRoom", "publishCircle",
				"publishRectangle", "publishPolyline", "publishPolygon",
				"addLike", "addDislike" ]
	});
	this.personalAdapter.coupleObject("room", this, {
		messageType : "STATE",
		explicitMapping : [ 'select' ],
		getElementState:function(){
			return {selected:this.selected};
		},
		setElementState:function(state){
			this._doSelect(state.selected);
		}

	});
};
Room.prototype._doSelect = function(id) {
	if(this.selected==id)
		return;
	this.selected = id;
	
	tl.getBand(0).setCenterVisibleDate(this.publicBlogs[id].date);
	var title = $.trim(this.publicBlogs[id].title);
	$("img", $("div.selected").removeClass("selected"))
			.attr(
					{
						src : "http://api.simile-widgets.org/timeline/2.3.1/images/dull-blue-circle.png"
					});
	;
	$(".timeline-event-label:contains('" + title + "')")
			.each(
					function() {
						if ($.trim($(this).html()) == title) {
							$("img", $(this).prev().addClass("selected"))
									.attr(
											{
												src : "http://api.simile-widgets.org/timeline/2.3.1/images/dull-red-circle.png"
											});
						}
					});
};
Room.prototype.select = function(id) {
	this._doSelect(id);
};
